package com.techsoul.module.pms.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialReceiptSaveParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialReceiptSearchParam;
import com.techsoul.module.pms.service.ITbWmsMaterialReceiptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 入库计划 Controller
 *
 * @author liuen
 * @date 2022-01-25
 */
@Api(tags = "入库计划")
@RestController
@RequestMapping("/web/tbWmsMaterialReceipt")
public class TbWmsMaterialReceiptController {

    @Autowired
    private Result result;
    
    @Autowired
    private ITbWmsMaterialReceiptService service;

    @ApiOperation("新增")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody TbWmsMaterialReceiptSaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:update')")
    @PutMapping
    public Result update(@Valid @RequestBody TbWmsMaterialReceiptSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody TbWmsMaterialReceiptSearchParam params) {
        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody TbWmsMaterialReceiptSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        if(!StringUtils.isNull(params.getStatus())){
            if(params.getStatus().contains(";")){

                List<String> statusList = new ArrayList<String>();
                String []statuss = params.getStatus().split(";");
                for(int i =0 ;i<statuss.length;i++){
                    statusList.add(statuss[i]);
                }
                params.setStatusList(statusList);
                params.setStatus(null);
            }
        }
        if(!StringUtils.isNull(params.getDateRange())){
            Date daterange = DateUtils.parseDate(params.getDateRange());
            Date beginDay = DateUtils.getDateBefore(daterange,1);
            String bgdate = DateUtils.formatDate(beginDay);
            params.setCreateTimeBegin(bgdate);
            Date endday = DateUtils.getDateAfter(daterange,1);
            String enddate = DateUtils.formatDate(endday);
            params.setCreateTimeEnd(enddate);
        }
        return result.success(this.service.findAll(params, current, pageSize));
    }
    @ApiOperation("随车工具入库接受")
    //@PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:toolsInsert')")
    @PostMapping("/insertToolsAll")
    public Result insertToolsAll(@RequestBody TbWmsMaterialReceiptSaveParam params){
        return result.success(service.insertALLToolsItem(params));
    }
    @ApiOperation("收货单接受")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:insert')")
    @PostMapping("/insertAll")
    public Result insertAll(@RequestBody TbWmsMaterialReceiptSaveParam params){
        return result.success(service.insertALLItem(params));
    }
    @ApiOperation("收货单接受")
    //@PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:newVehicleIn')")
    @PostMapping("/addNewVehicleReceipt")
    public Result addNewVehicleReceipt(@RequestBody TbWmsMaterialReceiptSaveParam params){
        return result.success(service.addNewVehicleReceipt(params));
    }

    @ApiOperation("新车入库修改")
    //@PreAuthorize("hasAnyAuthority('web:tbWmsMaterialReceipt:updataNewVehicleReceipt')")
    @PostMapping("/updataNewVehicleReceipt")
    public Result updataNewVehicleReceipt(@RequestBody TbWmsMaterialReceiptSaveParam params){
        return result.success(service.updataNewVehicleReceipt(params));
    }

}
